package com.wangdao.wechatmall.service.wx;

import com.github.pagehelper.PageHelper;
import com.wangdao.wechatmall.bean.pojo.*;
import com.wangdao.wechatmall.bean.vo.wx.HomeIndexVO;
import com.wangdao.wechatmall.mapper.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.List;

/**
 * @Description:
 * @Author: JK
 * @Date: create in 2021/7/11 16:11
 */
@Service
public class WXHomeServiceImpl implements WXHomeService{
    @Autowired
    GoodsMapper goodsMapper;

    @Autowired
    OrderGoodsMapper orderGoodsMapper;

    @Autowired
    CouponMapper couponMapper;

    @Autowired
    CategoryMapper categoryMapper;

    @Autowired
    GrouponMapper grouponMapper;

    @Autowired
    GrouponRulesMapper grouponRulesMapper;

    @Autowired
    AdMapper adMapper;

    @Autowired
    BrandMapper brandMapper;

    @Autowired
    TopicMapper topicMapper;

    /**
     * 展示商城主页
     * 1.查询goods列表 isNew=1
     * 2.查询coupon列表
     * 3.查询channel，即category列表pid=0
     * 4.查询groupon列表
     * 5.查询ad列表
     * 6.查询brand列表
     * 7.查询goods列表 isHot=1
     * 8.查询topic列表
     * 9.查询每种channel下的goods列表
     * @return
     */
    @Override
    public HomeIndexVO showHome() {
        HomeIndexVO homeIndexVO = new HomeIndexVO();

        // NewGoodsList
        PageHelper.offsetPage(0, 6);
        GoodsExample goodsExample = new GoodsExample();
        GoodsExample.Criteria goodsExampleCriteria = goodsExample.createCriteria();
        goodsExampleCriteria.andIsNewEqualTo(true);
        goodsExampleCriteria.andDeletedEqualTo(false);
        List<Goods> newGoods = goodsMapper.selectByExample(goodsExample);
        ArrayList<HomeIndexVO.NewGoodsListDTO> newGoodsListDTOS = new ArrayList<>();
        for (Goods newGood : newGoods) {
            HomeIndexVO.NewGoodsListDTO newGoodsListDTO = new HomeIndexVO.NewGoodsListDTO(newGood.getId(), newGood.getName(), newGood.getBrief(), newGood.getPicUrl(), newGood.getIsNew(), newGood.getIsHot(), newGood.getCounterPrice(), newGood.getRetailPrice());
            newGoodsListDTOS.add(newGoodsListDTO);
        }
        homeIndexVO.setNewGoodsList(newGoodsListDTOS);

        // CouponList
        PageHelper.offsetPage(0, 3);
        CouponExample couponExample = new CouponExample();
        CouponExample.Criteria couponExampleCriteria = couponExample.createCriteria();
        couponExampleCriteria.andDeletedEqualTo(false);
        couponExampleCriteria.andTypeNotEqualTo(Short.parseShort("2"));
        List<Coupon> coupons = couponMapper.selectByExample(couponExample);
        ArrayList<HomeIndexVO.CouponListDTO> couponListDTOS = new ArrayList<>();
        for (Coupon coupon : coupons) {
            HomeIndexVO.CouponListDTO couponListDTO = new HomeIndexVO.CouponListDTO(coupon.getId(), coupon.getName(), coupon.getDesc(), coupon.getTag(), coupon.getDiscount(), coupon.getMin(), coupon.getDays(), coupon.getStartTime(), coupon.getEndTime());
            couponListDTOS.add(couponListDTO);
        }
        homeIndexVO.setCouponList(couponListDTOS);

        // Channel
        CategoryExample categoryExample = new CategoryExample();
        CategoryExample.Criteria categoryExampleCriteria = categoryExample.createCriteria();
        categoryExampleCriteria.andPidEqualTo(0);
        categoryExampleCriteria.andDeletedEqualTo(0);
        List<Category> channels = categoryMapper.selectByExample(categoryExample);
        ArrayList<HomeIndexVO.ChannelDTO> channelDTOS = new ArrayList<>();
        for (Category channel : channels) {
            HomeIndexVO.ChannelDTO channelDTO = new HomeIndexVO.ChannelDTO(channel.getId(), channel.getName(), channel.getIconUrl());
            channelDTOS.add(channelDTO);
        }
        homeIndexVO.setChannel(channelDTOS);

        // 团购暂时未写
        // GrouponList
        PageHelper.offsetPage(0, 4);
        GrouponExample grouponExample = new GrouponExample();
        GrouponExample.Criteria grouponExampleCriteria = grouponExample.createCriteria();
        grouponExampleCriteria.andDeletedEqualTo(false);
        List<Groupon> groupons = grouponMapper.selectByExample(grouponExample);
        ArrayList<HomeIndexVO.GrouponListDTO> grouponListDTOS = new ArrayList<>();
        //ArrayList<Integer> grouponIds = new ArrayList<>();
        for (Groupon groupon : groupons) {
            // 1.根据groupon的rulesId查询grouponRules
            GrouponRules grouponRules = grouponRulesMapper.selectByPrimaryKey(groupon.getRulesId());
            // 2.根据grouponRules的goodsId查询goods
            Goods goods = goodsMapper.selectByPrimaryKey(grouponRules.getGoodsId());
            // 3.根据groupon的orderId查询orderGoods
            OrderGoods orderGoods = orderGoodsMapper.selectByPrimaryKey(groupon.getOrderId());

            // 计算团购价格 = RetailPrice - Discount
            BigDecimal grouponPrice = goods.getRetailPrice().subtract(grouponRules.getDiscount());
            // 将Goods转换为HomeIndexVO.GrouponListDTO.GoodsDTO
            HomeIndexVO.GrouponListDTO.GoodsDTO goodsDTO = new HomeIndexVO.GrouponListDTO.GoodsDTO(goods.getId(), goods.getName(), goods.getBrief(), goods.getPicUrl(), goods.getCounterPrice(), goods.getRetailPrice());
            // 获取团购人数
            Short groupon_member = orderGoods.getNumber();

            HomeIndexVO.GrouponListDTO grouponListDTO = new HomeIndexVO.GrouponListDTO(grouponPrice, goodsDTO, groupon_member);
            grouponListDTOS.add(grouponListDTO);
        }
        homeIndexVO.setGrouponList(grouponListDTOS);

        // Banner(ad)
        AdExample adExample = new AdExample();
        AdExample.Criteria adExampleCriteria = adExample.createCriteria();
        adExampleCriteria.andDeletedEqualTo(false);
        List<Ad> banners = adMapper.selectByExample(adExample);
        ArrayList<HomeIndexVO.BannerDTO> bannerDTOS = new ArrayList<>();
        for (Ad banner : banners) {
            HomeIndexVO.BannerDTO bannerDTO = new HomeIndexVO.BannerDTO(banner.getId(), banner.getName(), banner.getLink(), banner.getUrl(), banner.getPosition(), banner.getContent(), banner.getEnabled(), banner.getAddTime(), banner.getUpdateTime(), banner.getDeleted());
            bannerDTOS.add(bannerDTO);
        }
        homeIndexVO.setBanner(bannerDTOS);

        // BrandList
        PageHelper.offsetPage(0, 4);
        BrandExample brandExample = new BrandExample();
        BrandExample.Criteria brandExampleCriteria = brandExample.createCriteria();
        brandExampleCriteria.andDeletedEqualTo(0);
        List<Brand> brands = brandMapper.selectByExample(brandExample);
        ArrayList<HomeIndexVO.BrandListDTO> brandListDTOS = new ArrayList<>();
        for (Brand brand : brands) {
            HomeIndexVO.BrandListDTO brandListDTO = new HomeIndexVO.BrandListDTO(brand.getId(), brand.getName(), brand.getDesc(), brand.getPicUrl(), brand.getFloorPrice());
            brandListDTOS.add(brandListDTO);
        }
        homeIndexVO.setBrandList(brandListDTOS);

        // HotGoodsList
        PageHelper.offsetPage(0, 6);
        GoodsExample goodsExample1 = new GoodsExample();
        GoodsExample.Criteria goodsExample2Criteria = goodsExample1.createCriteria();
        goodsExample2Criteria.andIsHotEqualTo(true);
        goodsExample2Criteria.andDeletedEqualTo(false);
        List<Goods> hotGoods = goodsMapper.selectByExample(goodsExample1);
        ArrayList<HomeIndexVO.HotGoodsListDTO> hotGoodsListDTOS = new ArrayList<>();
        for (Goods hotGood : hotGoods) {
            HomeIndexVO.HotGoodsListDTO hotGoodsListDTO = new HomeIndexVO.HotGoodsListDTO(hotGood.getId(), hotGood.getName(), hotGood.getBrief(), hotGood.getPicUrl(), hotGood.getIsNew(), hotGood.getIsHot(), hotGood.getCounterPrice(), hotGood.getRetailPrice());
            hotGoodsListDTOS.add(hotGoodsListDTO);
        }
        homeIndexVO.setHotGoodsList(hotGoodsListDTOS);

        // TopicList
        PageHelper.offsetPage(0, 4);
        TopicExample topicExample = new TopicExample();
        TopicExample.Criteria topicExampleCriteria = topicExample.createCriteria();
        topicExampleCriteria.andDeletedEqualTo(false);
        List<Topic> topics = topicMapper.selectByExample(topicExample);
        ArrayList<HomeIndexVO.TopicListDTO> topicListDTOS = new ArrayList<>();
        for (Topic topic : topics) {
            HomeIndexVO.TopicListDTO topicListDTO = new HomeIndexVO.TopicListDTO(topic.getId(), topic.getTitle(), topic.getSubtitle(), topic.getPrice(), topic.getReadCount(), topic.getPicUrl());
            topicListDTOS.add(topicListDTO);
        }
        homeIndexVO.setTopicList(topicListDTOS);

        // FloorGoodsList
        ArrayList<HomeIndexVO.FloorGoodsListDTO> floorGoodsListDTOS = new ArrayList<>();
        for (Category channel : channels) {
            // 1.查询channel下的categories
            Integer channelId = channel.getId();
            CategoryExample categoryExample1 = new CategoryExample();
            CategoryExample.Criteria categoryExample1Criteria = categoryExample1.createCriteria();
            categoryExample1Criteria.andPidEqualTo(channelId);
            categoryExample1Criteria.andDeletedEqualTo(0);
            List<Category> categories = categoryMapper.selectByExample(categoryExample1);

            // 2.查询category下的goods
            if (categories.size() > 0) {
                PageHelper.offsetPage(0, 4);
                Integer categoryId = categories.get(0).getId();
                GoodsExample goodsExample2 = new GoodsExample();
                GoodsExample.Criteria goodsExample2Criteria1 = goodsExample2.createCriteria();
                goodsExample2Criteria1.andCategoryIdEqualTo(categoryId);
                goodsExample2Criteria1.andDeletedEqualTo(false);
                List<Goods> goodsOfChannel = goodsMapper.selectByExample(goodsExample2);

                // 将goods列表转换为HomeIndexVO.FloorGoodsListDTO.GoodsListDTO列表
                ArrayList<HomeIndexVO.FloorGoodsListDTO.GoodsListDTO> goodsListDTOS = new ArrayList<>();
                for (Goods goods : goodsOfChannel) {
                    HomeIndexVO.FloorGoodsListDTO.GoodsListDTO goodsListDTO = new HomeIndexVO.FloorGoodsListDTO.GoodsListDTO(goods.getId(), goods.getName(), goods.getBrief(), goods.getPicUrl(), goods.getIsNew(), goods.getIsHot(), goods.getCounterPrice(), goods.getRetailPrice());
                    goodsListDTOS.add(goodsListDTO);
                }

                HomeIndexVO.FloorGoodsListDTO floorGoodsListDTO = new HomeIndexVO.FloorGoodsListDTO(channel.getName(), goodsListDTOS, channelId);
                floorGoodsListDTOS.add(floorGoodsListDTO);
            }
        }
        homeIndexVO.setFloorGoodsList(floorGoodsListDTOS);

        return homeIndexVO;
    }
}
